import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class Countdown {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for (int i = 0; i < 3; i ++) {
            (new Thread(new MyThread(countDownLatch), "player " + i)).start();
        }
        System.out.println("init game");
        countDownLatch.await();
        System.out.println("start game");
    }

    static class MyThread implements Runnable {
        CountDownLatch countDownLatch = null;
        MyThread(CountDownLatch count) {
            this.countDownLatch = count;
        }
        @Override
        public void run() {
            Random rand = new Random();
            int randomNum = rand.nextInt((3000 - 1000) + 1) + 1000;//产生1000到3000之间的随机整数
            try {
                Thread.sleep(randomNum);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" 已经准备好了, 所使用的时间为 "+((double)randomNum/1000)+"s");
            this.countDownLatch.countDown();
        }
    }
}
